<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	cfc('core')->checklogin();
	$level=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level');
	if($level){
		$return['isvip']=1;
	}
	$return['nextlevel']=model('jy_weishop/member')->get_nextlevel(0,true);
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$post=array(
		'totalprice'=>0,
		'price'=>0.00,
		'username'=>$member['nickname'],
		'mobile'=>$member['mobile'],
		'diyfields'=>array(),
		'total'=>1,
		'type'=>0
	);
	$return['groups']=pdo_getall('jy_yxfrom_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id','paixu desc,id desc');
	foreach($return['groups'] as &$gv){
		$gv['list']=pdo_getall('jy_yxfrom_list',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'groupid'=>$gv['id']),array('id','title','desc','price','vip_price','order_types','diyfields'),'id');
		foreach($gv['list'] as &$lv){
			$lv['diyfields']=json_decode($lv['diyfields'],true);
			foreach($lv['diyfields'] as &$dv){
					if($dv['type']==4 || $dv['type']==5){
						$value=str_array($dv['value']);
						$dv['value']=array();
						foreach($value as $title){
							$dv['value'][]=array('title'=>$title);
						}
					}
			}
		}
	}
	if($_SESSION['wxapp_id']){
		$return['groups2']=pdo_getall('jy_yxfrom_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu desc,id desc');
		foreach($return['groups2'] as &$gv){
			$gv['list']=pdo_getall('jy_yxfrom_list',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'groupid'=>$gv['id']),array('id','title','desc','price','vip_price','order_types','diyfields'));
			foreach($gv['list'] as &$lv){
				$lv['diyfields']=json_decode($lv['diyfields'],true);
				foreach($lv['diyfields'] as &$dv){
					if($dv['type']==4 || $dv['type']==5){
						$value=str_array($dv['value']);
						$dv['value']=array();
						foreach($value as $title){
							$dv['value'][]=array('title'=>$title);
						}
					}
				}
			}
		}
	}
	$days=array();
	for($i=0;$i<15;$i++){
		$days[]=date("Y-m-d",strtotime("+".$i." day"));
	}
	$return['days']=$days;
	$daytime=date('Y-m-d');
	$start = mktime(8, 0);
	$end = mktime(20, 0);
	$times=array();
	for($i = $start; $i < $end;) {
		$times[]=date('H:i', $i);
		$i += 3600;
	}
	$return['times']=$times;
	if($_RQ['fuwuid']){
		$nowitem=pdo_get('jy_yxfrom_list',array('id'=>intval($_RQ['fuwuid']),'uniacid'=>$_SESSION['uniacid']));
		$nowitem['diyfields']=json_decode($nowitem['diyfields'],true);
		foreach($nowitem['diyfields'] as &$dv){
			if($dv['type']==4 || $dv['type']==5){
				$value=str_array($dv['value']);
				$dv['value']=array();
				foreach($value as $title){
					$dv['value'][]=array('title'=>$title);
				}
			}
		}
	}
	if($nowitem){
		$post['nowitem']=$nowitem;
		$post['groupid']=$nowitem['groupid'];
		$post['pid']=$nowitem['id'];
		$post['vip_price']=$nowitem['vip_price'];
		$post['price']=$nowitem['price'];
		$post['order_types']=$nowitem['order_types'];
		$post['remark']='';
		$post['diyfields']=$nowitem['diyfields'];
		$post['totalprice']=$nowitem['price'];
		foreach($return['groups2'] as $k=>$g2v){
			if($g2v['id']==$post['groupid']){
				$post['groupidindex']=$k;
				foreach($g2v['list'] as $k2=>$p2v){
					if($g2v['id']==$post['pid']){
						$post['pidindex']=$k2;
					}
				}
			}
		}
	}
	if(strpos($post['order_types'],';0;') !== false){ 
			$post['order_types0']=1;
	}
	if(strpos($post['order_types'],';1;') !== false){ 
			$post['order_types1']=1;
	}
	$post['day']=date('Y-m-d');
	$post['time']=date('H:i',time()+1800);
	$return['post']=$post;
	$return['buyxieyi']=$this->plugin_setting('xieyi');
	set_html('title','下单');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$pid = $postdata['pid'];
	$groupid = $postdata['groupid'];
	$buyxieyi=$this->plugin_setting('xieyi');
	if(!$postdata['checked'] && $buyxieyi['isopen']){
			exi('请先阅读协议！','error');
	} 
	if(!$postdata['username']){
			exi('请输入联系人姓名','error');
	}
	if(!$postdata['mobile']){
			exi('请输入联系人手机号码','error');
	}  
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	}
	if(!$groupid){
		exi('请选项维修类目！','error');
	}
	if(!$pid){
		exi('请选项维修项目！','error');
	}
	if($postdata['diyfields']){
			foreach ($postdata['diyfields'] as &$item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
				if($item['type']==14){
					$item['data'] = date('Y-m-d',strtotime($item['data']));
				}
			}
	}
	$postdata['address']=$postdata['address']?:$postdata['addressinfo']['address'];
	if($postdata['type']==0 && !$postdata['address']){
		//exi('请选择上门地址！','error');
	}
	if($postdata['type']==0 && !$postdata['day']){
		//exi('请选择上门时间！','error');
	}
	if($postdata['type']==1 && !$postdata['storeid']){
		exi('请选择门店！','error');
	}
	if($postdata['type']==1 && !$postdata['day']){
		exi('请选择到店时间！','error');
	}
	//判断待支付订单及判断是否超过可预订次数
	$serve=pdo_get('jy_yxfrom_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid));
	$level=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level');
	if($level){
		$totalprice=$serve['vip_price'];
	}else{
		$totalprice=$serve['price'];
	}
	$totalprice=$serve['price'];
	if(($return['pluginset']['is_dianfee']==1 && $postdata['type']==1) || $return['pluginset']['is_dianfee']==2){
		$isnopay=1;
	}else{
		if($postdata['yuejie_select']==1 && $this->plugin_exists('jy_yuejie')){
			//$yuejiemid=model('jy_yuejie/model')->checktype();
		}
	}
	$postdata['serve_time'] = $postdata['day'].' '.$postdata['time'];
	$serve_time=strtotime($postdata['serve_time']);
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'type'=>$postdata['type'],
			'worker_id'=>$postdata['worker_id'],
			'pid'=>$pid,
			'price'=>$totalprice,
			'payfee'=>$totalprice,
			'total'=>1,
			'address'=>$postdata['address'],
			'username'=>$postdata['username'],
			'mobile'=>$postdata['mobile'],
			'ordersn'=>get_ordersn('jy_yxfrom_order','ordersn'),
			'remark'=>$postdata['remark'],
			'images'=>ijson_encode($postdata['images']),
			'video'=>$postdata['video'],
			'thumb'=>$postdata['thumb'],
			'diyfields'=>ijson_encode($postdata['diyfields']),
			'serve_time'=>'',
			'storeid'=>$postdata['storeid'],
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yxfrom_order',$insert);
		$id=pdo_insertid();
		if($totalprice>0 && !$isnopay && !$yuejiemid){
			$params=array(
					'id'=>$id,
					'title'=>'维修订单',
					'fee'=>$totalprice,
					'plugin'=>'jy_yxfrom',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_yxfrom/fuwu.book/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_yxfrom/fuwu.order/',
					'error_url'=>'jy_yxfrom/fuwu.order/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
		}else{
			$payresult=array('fee'=>0);
			$order=$insert;
			$order['id']=$id;
			$this->model('model')->payresult($order,$payresult,'',$yuejiemid);
			$this->model('notice')->notice_fuwuorder($order);	
			exi('预约成功！','','jy_yxfrom/fuwu.order/');
		}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_yxfrom_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['payfee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$this->model('model')->payresult($order,$payresult,1);
	$this->model('notice')->notice_fuwuorder($order);	
	exi('支付成功','success','jy_yxfrom/fuwu.order/list/status=1&id='.$order['id']);
}